package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesProductRecords;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesProductRecordsDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:34:14
 */
public class MesProductRecordsConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesProductRecordsConverts.class);

    public static List<MesProductRecordsDTO> convert(List<MesProductRecords> mesProductRecordsList) {
        if (CollectionUtils.isEmpty(mesProductRecordsList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesProductRecordsDTO> mesProductRecordsListResult = Lists.newArrayList();
        for (MesProductRecords mesProductRecords : mesProductRecordsList) {
            mesProductRecordsListResult.add(convert(mesProductRecords));
        }
        return mesProductRecordsListResult;
    }

    public static MesProductRecordsDTO convert(MesProductRecords mesProductRecords) {
        try {
            return BeanUtils.copy(mesProductRecords, MesProductRecordsDTO.class);
        } catch (Exception e) {
            LOG.error("MesProductRecords到MesProductRecordsDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProductRecords到MesProductRecordsDTO转换失败", e);
        }
    }

    public static MesProductRecords convert(MesProductRecordsDTO mesProductRecordsDTO) {
        try {
            return BeanUtils.copy(mesProductRecordsDTO, MesProductRecords.class);
        } catch (Exception e) {
            LOG.error("MesProductRecordsDTO到MesProductRecords转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProductRecordsDTO到MesProductRecords转换失败", e);
        }
    }
}